package com.wanpinghui.wphlibrary.model.oauth;

import com.wanpinghui.wphlibrary.common.IWPHApi;
import com.wanpinghui.wphlibrary.model.oauth.bean.LoginResponseBean;
import com.wanpinghui.wphlibrary.model.oauth.bean.UserBean;
import com.yunshl.yunshllibrary.storage.MDBManager;
import com.yunshl.yunshllibrary.utils.TextUtil;

import java.util.HashMap;
import java.util.Map;

import retrofit.Network;
import retrofit.ToKenUtil;
import retrofit.callback.YRequestCallback;
import retrofit.callback.YunShlAction;
import retrofit.callback.YunShlResult;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/***
 * 功能描述:
 * 作者:qiujialiu
 * 时间:2017/11/22
 ***/

public class OAuthServiceImp implements OAuthService {
    private int userId;
    @Override
    public void sendLoginCode(String mobile, final YRequestCallback callback) {
        Network.getApi(IWPHApi.class)
                .sendLoginCaptcha(mobile)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<YunShlResult<Object>>() {
                    @Override
                    public void call(YunShlResult<Object> result) {
                        if (result.isSuccess()) {
                            if (callback != null) {
                                callback.onSuccess(null);
                            }
                        }else {
                            if (callback != null) {
                                callback.onFailed(0,result.msg);
                            }
                        }
                    }
                }, new YunShlAction<Throwable>(callback));
    }

    @Override
    public void login(String mobile, String code, final YRequestCallback<LoginResponseBean> callback) {
        Map<String, Object> map = new HashMap<>();
        map.put("mobile", mobile);
        map.put("sms_captcha", code);
        Network.getApi(IWPHApi.class)
                .login(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<YunShlResult<LoginResponseBean>>() {
                    @Override
                    public void call(YunShlResult<LoginResponseBean> result) {
                        if (result.isSuccess()) {
                            ToKenUtil.saveToken(result.body.getToken());
                            UserBean userBean = UserInfoUtil.getUserInfo();
                            userId = 0;
                            if (result.body.getUserBean() != null) {
                                if (userBean != null) {
                                    userBean.setUser(result.body.getUserBean());
                                    UserInfoUtil.saveUserInfo(userBean);
                                }else {
                                    UserBean userBean2 = new UserBean();
                                    userBean2.setUser(result.body.getUserBean());
                                    UserInfoUtil.saveUserInfo(userBean2);
                                }
                                MDBManager.getInstance().createDatabase(result.body.getUserBean().getId(),"wph_supplier");
                            }
                            if (callback != null) {
                                callback.onSuccess(result.body);
                            }
                        }else {
                            if (callback != null) {
                                if (result.status == 41002006) {
                                    callback.onFailed(41002006, "短信验证码不存在");
                                }else if (result.status == 41002022) {
                                    callback.onFailed(41002022, "验证码错误");
                                }else {
                                    callback.onFailed(0, result.msg);
                                }
                            }
                        }
                    }
                }, new YunShlAction<Throwable>(callback));
    }

    @Override
    public void certifySupplier(String nickName, int gender, String address, String company, String position, final YRequestCallback<UserBean> callback) {
        Map<String, Object> map = new HashMap<>();
        map.put("nickname", nickName);
        map.put("gender", gender);
        if (TextUtil.isNotEmpty(company)) {
            map.put("com", company);
        }
        Network.getApi(IWPHApi.class)
                .certirySupplier(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<YunShlResult<UserBean>>() {
                    @Override
                    public void call(YunShlResult<UserBean> result) {
                        UserBean userBean = UserInfoUtil.getUserInfo();
                        if (userBean != null) {
                            userBean.setUser(result.body.getUser());
                        }
                        UserInfoUtil.saveUserInfo(userBean);
                        if (result.isSuccess()) {
                            if (callback != null) {
                                callback.onSuccess(result.body);
                            }
                        }else {
                            if (callback != null) {
                                callback.onFailed(0, result.msg);
                            }
                        }
                    }
                }, new YunShlAction<Throwable>(callback));
    }

    @Override
    public UserBean getUserInfo(YRequestCallback<UserBean> callback) {
        UserBean userBean = UserInfoUtil.getUserInfo();
        if (callback != null) {
            getUserInfoFromServer(callback);
        }
        if (userBean != null) {
            return userBean;
        }
        return null;
    }

    @Override
    public void getUserInfoFromS(YRequestCallback<UserBean> callback) {
        getUserInfoFromServer(callback);
    }

    private void getUserInfoFromServer(final YRequestCallback<UserBean> callback) {
        Network.getApi(IWPHApi.class)
                .getUserInfo()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<YunShlResult<UserBean>>() {
                    @Override
                    public void call(YunShlResult<UserBean> result) {
                        UserInfoUtil.saveUserInfo(result.body);
                        if (result.isSuccess()) {
                            UserInfoUtil.saveUserInfo(result.body);
                            userId = result.body.getUser().getIntId();
                            if (callback != null) {
                                callback.onSuccess(result.body);
                            }
                        }else {
                            if (callback != null) {
                                callback.onFailed(0, result.msg);
                            }
                        }
                    }
                }, new YunShlAction<Throwable>(callback));
    }

    @Override
    public String getToken() {
        return ToKenUtil.getToken();
    }

    @Override
    public void logout() {
        ToKenUtil.deleteToken();
        UserInfoUtil.deleteUserInfo();
        userId = 0;
    }

    @Override
    public int getUserId() {
        if (userId != 0) {
            return userId;
        }else {
            UserBean userBean = UserInfoUtil.getUserInfo();
            if (userBean != null) {
                userId = userBean.getUser().getIntId();
                return userId;
            }
            return 0;
        }
    }


}
